# Create your views here.
from django.contrib.auth.models import User
from django.http import HttpResponse, HttpResponseRedirect
from django.template import loader, RequestContext
from django.contrib.auth.decorators import login_required
from django import forms
from django.contrib.auth.forms import UserCreationForm
from django.shortcuts import render_to_response
from registration.forms import RegistrationForm
from addons.models import addon


@login_required
def index(request):
    try:
        u = User.objects.get(id=int(request.GET['id']))
    except Exception, e:
        t = loader.get_template('profile/no_user.html')
        c = RequestContext(request, {'error': e})
    else:
        addon_list = addon.objects.filter(addon_author_id=request.GET ['id'])
        t = loader.get_template('profile/index.html')
        c = RequestContext(request, {'u': u, 'addon_list': addon_list})
    return HttpResponse(t.render(c))


@login_required
def edit_profile(request):
    t = loader.get_template('profile/edit.html')
    c = RequestContext(request)
    return HttpResponse(t.render(c))


def register(request):
    if request.method == 'POST':
        form = RegistrationForm(request.POST)
        if form.is_valid():
            new_user = form.save()
            return HttpResponseRedirect("/addons/")
    else:
        form = UserCreationForm()
    return render_to_response("registration/register.html", {'form': form,}, context_instance=RequestContext(request))
